package com.gotye.particlesystem;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.gotye.particlesystem.gles.GLRenderer;
import com.gotye.particlesystem.gles.ParticlesRenderer;

public class ParticlesActivity extends AppCompatActivity {
    private GLSurfaceView glSurfaceView;
    private boolean mbRendererSet;

    //private ParticlesRenderer myRenderer = new ParticlesRenderer(this);
    private GLRenderer myRenderer = new GLRenderer(this);

    private Thread renderThread;
    private boolean running;

    private GestureDetector mGestureDetector;
    private ScaleGestureDetector mScaleGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_main);

        glSurfaceView = (GLSurfaceView)this.findViewById(R.id.view);

        final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        final ConfigurationInfo info = am.getDeviceConfigurationInfo();
        final boolean isSupportEs2 = (info.reqGlEsVersion >= 0x20000);

        if (isSupportEs2) {
            glSurfaceView.setEGLContextClientVersion(2);
            glSurfaceView.setRenderer(myRenderer);
            glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
            mbRendererSet = true;
        }
        else {
            Toast.makeText(this, "Not support gles2", Toast.LENGTH_SHORT).show();
            return;
        }

        renderThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (running) {
                    glSurfaceView.requestRender();
                    try {
                        Thread.sleep(40);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        });

        running = true;
        renderThread.start();

        glSurfaceView.setLongClickable(true);
        glSurfaceView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mGestureDetector.onTouchEvent(event);

                mScaleGestureDetector.onTouchEvent(event);

                return true;
            }
        });

        mGestureDetector =
                new GestureDetector(ParticlesActivity.this, new GestureDetector.SimpleOnGestureListener() {

                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                            float distanceX, float distanceY) {
                        myRenderer.rotate(-distanceY / 4f, -distanceX / 4f, 0f);
                        return true;
                    }

                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        myRenderer.toggleDisplay();
                        return true;
                    }
                });

        mScaleGestureDetector =
                new ScaleGestureDetector(ParticlesActivity.this, new ScaleGestureDetector.OnScaleGestureListener() {

                    @Override
                    public boolean onScaleBegin(ScaleGestureDetector detector) {
                        return true;
                    }

                    @Override
                    public void onScaleEnd(ScaleGestureDetector detector) {

                    }

                    @Override
                    public boolean onScale(ScaleGestureDetector detector) {
                        float scaleFactor = detector.getScaleFactor();
                        myRenderer.scale(scaleFactor);
                        return true;
                    }
                });
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (renderThread != null) {
            running = false;
            renderThread.interrupt();
            try {
                renderThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if (mbRendererSet) {
            glSurfaceView.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (mbRendererSet) {
            glSurfaceView.onResume();
        }
    }

}
